﻿using ChoMy.Constants;
using ChoMy.Helpers;
using ChoMy.Models;
using ChoMy.Models.Data;
using PagedList;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChoMy.Controllers
{
    public class EventController : Controller
    {
        private ChoMyContext db = new ChoMyContext();
        private StringHelper stringHelper = new StringHelper();

        public EventController()
        {
            ViewBag.AltGlobal = GlobalConstant.AltGlobal();
            ViewBag.DeadlineFormat = "Kết thúc vào {0:d/M} lúc {0:h:mm tt}";
        }

        public ActionResult Index(string categoryNameId)
        {
            if (String.IsNullOrWhiteSpace(categoryNameId))
            {
                return RedirectToAction("BadRequest", "Error");
            }

            Category category = db.Categories
                .FirstOrDefault(x => x.NameId == categoryNameId
                    && x.ParentId == null
                    && !x.IsDelete);

            if (category == null)
            {
                return RedirectToAction("NotFound", "Error");
            }

            ViewBag.Category = category;
            ViewBag.UpcomingDate = GetUpcomingDate(category.Id);

            return View(GetPresentEvent(category.Id));
        }

        public ActionResult Details(string categoryNameId, string eventNameId, int? page, int? pageSize)
        {
            if (String.IsNullOrWhiteSpace(eventNameId))
            {
                return RedirectToAction("BadRequest", "Error");
            }

            Event eventt = db.Events
                .FirstOrDefault(x => x.NameId == eventNameId
                    && !x.IsDelete);

            if (eventt == null)
            {
                return RedirectToAction("NotFound", "Error");
            }

            if (eventt.Category.NameId != categoryNameId)
            {
                return RedirectToActionPermanent("Details", new 
                { 
                    categoryNameId = eventt.Category.NameId, 
                    eventNameId = eventNameId 
                });
            }

            ViewBag.Event = eventt;

            int pageNumber = page ?? 1;
            pageSize = pageSize ?? 30;

            IEnumerable<Product> products = eventt.EventProducts.Select(x => x.Product);

            return View(products.ToPagedList(pageNumber, (int)pageSize));
        }

        public ActionResult _AjaxUpcomingEvent(DateTime date, int categoryId)
        {
            ViewBag.Category = db.Categories.Find(categoryId);

            return PartialView(GetUpcomingEvent(date, categoryId));
        }

        public ActionResult _EventItem(Event eventt)
        {
            return PartialView(eventt);
        }

        #region Helpers
        private List<Event> GetUpcomingEvent(DateTime date, int categoryId)
        {
            return db.Events
                .Where(x => !x.IsDelete
                    && x.CategoryId == categoryId
                    && DbFunctions.TruncateTime(x.BeginDate) == date)
                .ToList();
        }

        private List<Event> GetPresentEvent(int? categoryId)
        {
            return db.Events
                .Where(x => x.CategoryId == categoryId
                    && !x.IsDelete
                    && x.BeginDate <= DateTime.Now
                    && DateTime.Now < x.EndDate)
                .OrderBy(x => x.EndDate)
                .ToList();
        }

        private List<DateTime> GetUpcomingDate(int? categoryId)
        {
            return db.Events
                .Where(x => x.CategoryId == categoryId
                   && !x.IsDelete
                   && x.BeginDate > DateTime.Now)
                .ToList()
                .GroupBy(x => x.BeginDate.Date)
                .Select(x => x.Key)
                .OrderBy(x => x)
                .ToList();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }

            base.Dispose(disposing);
        }
        #endregion
    }
}